<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Api extends CI_Controller {

		
	public function index(){
		show_404();
	}	

	
	public function redirect()
	{
	
		
		
		if(isset($_GET['access_token']) || isset($_GET['subscriber_number'])){
		//MOBILE
			$this->updateLog("MOBILE->"."Subscriber Consent->"."acces_token:".$_GET['access_token']."->subscriber_number:".$_GET['subscriber_number']);
			
			$response = $this->connection_model->api_redirect($_GET['access_token'],$_GET['subscriber_number']);
			$this->updateLog("MOBILE->"."DB Insert Response:".json_encode($response));
			
			if($response){
				redirect('tokenrequest/success');
			}else{
				redirect('tokenrequest/fail');
			}
			
			
		}else if(isset($_GET['code'])){
		//WEB
			$this->updateLog('WEB->'.'Subscriber Consent->'.'code:'.$_GET['code']);
			$result = $this->gettoken($_GET['code']);
			$this->updateLog("WEB->"."GET TOKEN->"."code:".json_encode($result));
			
			$response = $this->connection_model->api_redirect($result['access_token'],$result['subscriber_number']);
			$this->updateLog("WEB->"."DB Insert Response:".json_encode($response));
			
			if(response){
				redirect('tokenrequest/success');
			}else{
				redirect('tokenrequest/fail');
			}
			
		
		
		}else{
			redirect('tokenrequest/fail');
		}
		
	}
	
	private function gettoken($code)
	{
		
		$this->config->load("config", true);
		$url = "https://developer.globelabs.com.ph/oauth/access_token";
		$postvars = "app_id=" . "eALxHkKa8RfA7ixgMpca6zfazAkyHrag" . "&app_secret=" ."1a770eb6baac8dec15cb15d8edca7bfe0063655490a97067c0a9901c4d665535" ."&code=" . $code;
		
		$temp = curl_init($url);
		curl_setopt($temp, CURLOPT_POST, 1);
		curl_setopt($temp, CURLOPT_POSTFIELDS, $postvars);
		curl_setopt($temp, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($temp, CURLOPT_HEADER, 0);
		curl_setopt($temp, CURLOPT_RETURNTRANSFER, 1);
		
		if (! $data = curl_exec($temp)) { 
		
		} else {
		    $result = json_decode($data, true);
		    // print_r('<pre>');
		   	// print_r($result);
		   	// print_r('</pre>');
			return $result;
		}
		curl_close($temp); 
		
	}
	
	private function test($code='')
	{
		
		$this->config->load("config", true);
		$url = "https://developer.globelabs.com.ph/oauth/access_token";
		$postvars = "app_id=" . "eALxHkKa8RfA7ixgMpca6zfazAkyHrag" . "&app_secret=" ."1a770eb6baac8dec15cb15d8edca7bfe0063655490a97067c0a9901c4d665535" ."&code=" . $code;
		
		$temp = curl_init($url);
		curl_setopt($temp, CURLOPT_POST, 1);
		curl_setopt($temp, CURLOPT_POSTFIELDS, $postvars);
		curl_setopt($temp, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($temp, CURLOPT_HEADER, 0);
		curl_setopt($temp, CURLOPT_RETURNTRANSFER, 1);
		
		if (! $data = curl_exec($temp)) { 
		
		} else {
		    $result = json_decode($data, true);
		    // print_r('<pre>');
		   	// print_r($result);
		   	// print_r('</pre>');
			return $result;
		}
		curl_close($temp); 
		
	}
	
	private function updateLog($stringData){
		file_put_contents("logs.txt", $stringData , FILE_APPEND);
	}

	
	

	
}